<?php
namespace app\api\controller\new;

use think\Controller;
use think\Request;
use app\common\model\customer\Customer as CustomerModel;
use think\Db;
use app\common\model\power\Rolepower;

class Customer extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('303002'); // 客户管理权限
    }
    private function is_login()
    {
        $data = \app\common\model\user\User::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    // 客户列表/添加客户（303002）
    public function customerList(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser) {
            return json(['code'=>0, 'msg'=>'未登录']);
        }
        $role = $loginUser['role'];
        $user_id = $loginUser['id'];
        if ($role == 304) { // 代理商员工
            $customer_ids = Db::name('user_customer')->where('user_id', $user_id)->column('customer_id');
        } elseif ($role == 303) { // 代理商管理员
            $agency_id = Db::name('user_agency')->where('user_id', $user_id)->value('agency_id');
            $emp_ids = Db::name('user_agency')->where('agency_id', $agency_id)->column('user_id');
            $customer_ids = Db::name('user_customer')->whereIn('user_id', $emp_ids)->column('customer_id');
        } else {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $list = CustomerModel::whereIn('CustomerId', $customer_ids)->select();
        return json(['code'=>1, 'msg'=>'success', 'data'=>$list]);
    }

    // 添加客户
    public function customerAdd(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser) {
            return json(['code'=>0, 'msg'=>'未登录']);
        }
        $data = $request->post();
        if (empty($data['Name']) || empty($data['Address']) || empty($data['OtherTel']) || empty($data['UserName'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $customer = new CustomerModel();
        $customer->Name = $data['Name'];
        $customer->Address = $data['Address'];
        $customer->OtherTel = $data['OtherTel'];
        $customer->UserName = $data['UserName'];
        $customer->AddTime = date('Y-m-d H:i:s');
        $customer->save();
        // 关联到当前用户
        Db::name('user_customer')->insert([
            'user_id' => $loginUser['id'],
            'customer_id' => $customer->CustomerId,
            'create_time' => time()
        ]);
        return json(['code'=>1, 'msg'=>'添加成功']);
    }

    // 代理商管理员分配客户给员工（303001）
    public function customerChangeAgency(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 303) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $user_id = $request->post('user_id');
        $customer_id = $request->post('customer_id');
        if (empty($user_id) || empty($customer_id)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        // 重新分配
        Db::name('user_customer')->where('customer_id', $customer_id)->update(['user_id' => $user_id]);
        return json(['code'=>1, 'msg'=>'分配成功']);
    }

    // 销售将代理商客户维护为自己客户（305003）
    public function customerChange(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $user_id = $request->post('user_id');
        $customer_id = $request->post('customer_id');
        if (empty($user_id) || empty($customer_id)) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        // 维护为自己客户
        Db::name('user_customer')->where('customer_id', $customer_id)->update(['user_id' => $user_id]);
        return json(['code'=>1, 'msg'=>'维护成功']);
    }
} 